

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;using THSERP.BusinessEntities;using THSERP.BaseProvider;
using System.Windows.Forms;
using THSLib;using THSERP.BaseProvider;

namespace THSERP.Modules.PriceList
{
    public class ARPriceListDetailsGridControl : THSGridControl
    {
        public override void InitGridControlDataSource()
        {
            //base.InitGridControlDataSource();
            PriceListEntities entity = (PriceListEntities)((BaseModuleERP)Screen.Module).CurrentModuleEntity;
            BindingSource bds = new BindingSource();
            bds.DataSource = entity.ARPriceListDetailsList;
            this.DataSource = bds;
        }

        protected override DevExpress.XtraGrid.Views.Grid.GridView InitializeGridView()
        {
            DevExpress.XtraGrid.Views.Grid.GridView gridView= base.InitializeGridView();
            gridView.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.Bottom;

            //Set all column is enable
            foreach (DevExpress.XtraGrid.Columns.GridColumn column in gridView.Columns)
            {
                column.OptionsColumn.AllowEdit = true;
            }

            return gridView;
        }

        protected override void GridView_InitNewRow(object sender, DevExpress.XtraGrid.Views.Grid.InitNewRowEventArgs e)
        {
            base.GridView_InitNewRow(sender, e);

            DevExpress.XtraGrid.Views.Grid.GridView gridView = sender as DevExpress.XtraGrid.Views.Grid.GridView;
            if (gridView.RowCount > 1)
            {
                int iLastCurrencyID = Convert.ToInt32(gridView.GetRowCellValue(gridView.RowCount - 2, gridView.Columns["FK_GECurrencyID"]));
                DateTime dtLastPriceListDetailsFromDate = Convert.ToDateTime(gridView.GetRowCellValue(gridView.RowCount - 2, "ARPriceListDetailFromDate"));
                DateTime dtLastPriceListDetailsToDate = Convert.ToDateTime(gridView.GetRowCellValue(gridView.RowCount - 2, "ARPriceListDetailToDate"));
                double dbLastPriceListDetailsFromQuantity = Convert.ToDouble(gridView.GetRowCellValue(gridView.RowCount - 2, "ARPriceListDetailFromQuantity"));
                double dbLastPriceListDetailsToQuantity = Convert.ToDouble(gridView.GetRowCellValue(gridView.RowCount - 2, "ARPriceListDetailTolQuantity"));

                //Set new row value
                gridView.SetRowCellValue(e.RowHandle, "FK_GECurrencyID", iLastCurrencyID);
                gridView.SetRowCellValue(e.RowHandle, "ARPriceListDetailFromDate", dtLastPriceListDetailsFromDate);
                gridView.SetRowCellValue(e.RowHandle, "ARPriceListDetailToDate", dtLastPriceListDetailsToDate);
                gridView.SetRowCellValue(e.RowHandle, "ARPriceListDetailFromQuantity", dbLastPriceListDetailsFromQuantity);
                gridView.SetRowCellValue(e.RowHandle, "ARPriceListDetailToQuantity", dbLastPriceListDetailsToQuantity);
            }
            else
            {
                gridView.SetRowCellValue(e.RowHandle, "FK_GECurrencyID", ((GECurrenciesInfo)new GECurrenciesController().GetFirstObject()).GECurrencyID);
                gridView.SetRowCellValue(e.RowHandle, "ARPriceListDetailFromDate", DateTime.Now.Date);
                gridView.SetRowCellValue(e.RowHandle, "ARPriceListDetailToDate", THSUtil.GetYearEndDate());
            }
        }

        // adds, 03/04/2008, validate row before leaving row
        protected override void GridView_ValidateRow(object sender, DevExpress.XtraGrid.Views.Base.ValidateRowEventArgs e)
        {
            base.GridView_ValidateRow(sender, e);

            PriceListEntities entity = (PriceListEntities)((BaseModuleERP)Screen.Module).CurrentModuleEntity;
            DevExpress.XtraGrid.Views.Grid.GridView gridView = (DevExpress.XtraGrid.Views.Grid.GridView)this.MainView;
            int iIndex = gridView.GetDataSourceRowIndex(e.RowHandle);

            if (DateTime.Compare(entity.ARPriceListDetailsList[iIndex].ARPriceListDetailFromDate, entity.ARPriceListDetailsList[iIndex].ARPriceListDetailToDate) > 0)
            {
                MessageBox.Show("Start date must be less than end date", "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
                ARPriceListDetailsController objARPriceListDetailsController = new ARPriceListDetailsController();
                ARPriceListDetailsInfo objARPriceListDetailsInfo = (ARPriceListDetailsInfo)objARPriceListDetailsController.GetObjectByID(entity.ARPriceListDetailsList[iIndex].ARPriceListDetailID);
                entity.ARPriceListDetailsList[iIndex].ARPriceListDetailFromDate = objARPriceListDetailsInfo.ARPriceListDetailFromDate;
                entity.ARPriceListDetailsList[iIndex].ARPriceListDetailToDate = objARPriceListDetailsInfo.ARPriceListDetailToDate;
                this.RefreshDataSource();
            }
        }

    }
}
